﻿<%@ WebHandler Language="C#" Class="Login" %>

using System;
using System.Web;
using System.Web.SessionState;
public class Login : IHttpHandler,IRequiresSessionState {
   
    public void ProcessRequest (HttpContext context) {
        
        context.Response.ContentType = "text/plain";
        string name = context.Request["UserName"].ToString();
        string password = context.Request["PassWord"].ToString();
        UserHelper u = new UserHelper(name,password);
        string IPretrun = GetIPAddress();
        if (IPretrun.Trim() == null || IPretrun.Trim() == "")
        {
            IPretrun = "未知IP地址" + getIp() + "  上次登录时间：" + DateTime.Now.ToString();
        }
        else
        {
            IPretrun = string.Format("{0}  {1} 上次登录时间：{2}", IPretrun,getIp(), DateTime.Now.ToString());
        }
        try
        {
            if (u.Login())
            {
                context.Session["name"] = name;
                context.Session["IP"] = u.FindAdminAddress(IPretrun, name);
                context.Response.Write(1);
            }
            else
            {
                context.Response.Write(0);
            }
        }
        catch
        {
            context.Response.Write(0); 
        }
    }
    protected string GetIPAddress()
    {
        return IpSearch.GetAddressWithIP(getIp());
    }
    private static string getIp()
    {
        if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            return System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(new char[] { ',' })[0];
        else
            return System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}